﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RZDropPoint : MonoBehaviour
{
    [SerializeField] private List<Transform> drops;

    [HideInInspector] public List<DropChild> childs;
    // 检测离 player 的距离 
    private void Awake ()
    {
        childs = new List<DropChild> ();
        for (int i = 0;i < drops.Count;i++)
        {
            DropChild dc = new DropChild (drops[i].GetChild(0));
            childs.Add (dc);
        }
    }

    // 发送事件(handle, this) 到 player 
}

public class DropChild
{
    public Transform child;

    public bool isEnabled;

    SpriteRenderer sr;

    public bool GetDist(Vector3 pos, float dist)
    {
        float distf = Vector3.Distance (child.position, pos);
        distf = distf > 0 ? distf : distf * -1f;
        Debug.Log ($"{distf} {dist}");
        return distf <= dist;
    }

    // 有遮挡返回 false
    public bool IsShield(Vector3 pos, LayerMask layer)
    {
        RaycastHit2D hit;
        float distf = Vector3.Distance (child.position, pos);
        distf = distf > 0 ? distf : distf * -1f;
        Debug.DrawLine (child.position, pos, Color.red, 1000);
        hit = Physics2D.Raycast (child.position, pos, distf, layer);
        //Debug.Log ($"{hit.collider == null}");
        return hit.collider == null;
    }

    public DropChild(Transform c)
    {
        child = c;
        isEnabled = true;
        sr = child.gameObject.GetComponent<SpriteRenderer> ();
    }

    public void ChangeColor()
    {
        if (sr != null) sr.color = Color.green;
    }

    public void RestoreColor()
    {
        if (sr != null) sr.color = Color.white;
    }
}
